﻿using Castle.Windsor;
using Castle.Windsor.Installer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WJP.Logging;

namespace WJP.DataPortal.WCF
{
    /// <summary>
    /// 路由服务
    /// </summary>
    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, InstanceContextMode = InstanceContextMode.PerCall, UseSynchronizationContext = false, ValidateMustUnderstand = false, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class RoutingService : IRoutingService, IHost
    {
        /// <summary>
        /// 开始请求
        /// </summary>
        /// <param name="message"></param>
        /// <param name="callback"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IAsyncResult BeginProcessRequest(Message message, AsyncCallback callback, object state)
        {
            return new AsyncResult(this, message, callback, state);
        }

        /// <summary>
        /// 结束请求
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public Message EndProcessRequest(IAsyncResult result)
        {
            AsyncResult asyncResult = result as AsyncResult;
            if (asyncResult.Error != null)
                throw asyncResult.Error;
            return asyncResult.Reply;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public Message Invoke(Message msg)
        {
            return new Invoker(msg).Invoke();
        }

        /// <summary>
        /// 注册Host
        /// </summary>
        /// <param name="host"></param>
        public void RegisterHost(SubHost host)
        {
            try
            {
                HostManager.Default.RegisterHost(host);
                LogManager.Logger.Info("服务：{0}注册成功".FormatArgs(host.Address));
            }
            catch (Exception ex)
            {
                LogManager.Logger.Error("RegisterHost失败:" + host ?? host.Address, ex);
            }
        }

        /// <summary>
        /// 获取全部Host信息
        /// </summary>
        /// <returns></returns>
        public IList<HostInfo> GetHostInfos()
        {
            try
            {
                return HostManager.Default.Hosts;
            }
            catch (Exception ex)
            {
                LogManager.Logger.Error("GetHostInfo失败", ex);
                return null;
            }
        }

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="address">地址</param>
        /// <returns></returns>
        public bool Start(string address)
        {
            return HostManager.Default.Start(address);
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="address">地址</param>
        /// <returns></returns>
        public bool Stop(string address)
        {
            return HostManager.Default.Stop(address);
        }
    }
}
